package com.edgclub.edg.activity;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.GroupMenberAdapter;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.GroupBeanData;
import com.edgclub.edg.model.result.RunData;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 支付管理
 */
public class GroupDetailsActivity extends GzBaseTooolBarActivity implements
        OnClickListener ,VolleyListener{

    private ImageView mGroup_icon;
    private LinearLayout mDetails_layout;
    private TextView mGroup_details;
    private TextView mP_layout;
    private com.hyphenate.easeui.widget.EaseExpandGridView mGridview;
    private LinearLayout mGroup_more;
    private Button mAdd_group;
    private GroupMenberAdapter menberAdapter;

    private UserPrefUtils userPrefUtils;
    private String groupid;
    private List<User>mUserList;
    private GroupBeanData mGroupBeanData;
    @Override
    public int getContentView() {
        return R.layout.activity_group_details;
    }
    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText(getIntent().getStringExtra("groupname")+"");
        main_back.setOnClickListener(this);
    }
    @Override
    public void initView() {
        mGroup_icon = (ImageView) findViewById(R.id.group_icon);
        mDetails_layout = (LinearLayout) findViewById(R.id.details_layout);
        mGroup_details = (TextView) findViewById(R.id.group_details);
        mP_layout = (TextView) findViewById(R.id.p_layout);
        mGridview = (com.hyphenate.easeui.widget.EaseExpandGridView) findViewById(R.id.gridview);
        mGroup_more = (LinearLayout) findViewById(R.id.group_more);
        mAdd_group = (Button) findViewById(R.id.add_group);
        mAdd_group.setOnClickListener(this);
        mGroup_more.setOnClickListener(this);
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).register(this);
    }
    @Override
    public void initData() {
        mUserList=new ArrayList<>();
        userPrefUtils = new UserPrefUtils(this);
        groupid=getIntent().getStringExtra("groupid");
        menberAdapter=new GroupMenberAdapter(this);

        getGroupDetails();
    }



    @Override
    public void onClick(View v) {
        if(mGroupBeanData==null)return;
        switch (v.getId()) {
            case R.id.add_group:
                if(!mGroupBeanData.getData().isJoin())
                     addGroup(true);
                else {

                    Intent intent = new Intent(this, ChatActivity.class);
//                    intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_GROUP);
//                    intent.putExtra(Constant.EXTRA_USER_ID, mGroupBeanData.getData().getGimid());
                    intent.putExtra("chatBean", RunData.getChatBeanMode(mGroupBeanData.getData(),null));
                    startActivity(intent);
                }
                break;
            case R.id.group_more:
                Intent intent=new Intent(this, GroupMembersActivity.class);
                intent.putExtra("groupid",groupid);
                intent.putExtra("gneme",mGroupBeanData.getData().getGname());
                startActivity(intent);
                break;
            case R.id.main_back:
                finish();
                break;
            default:
                break;
        }
    }
    @Subscribe
    public void refreshListView(HashMap<String, Object> map){
        String event = (String) map.get("event");
        boolean e=false;
        try {
             e= (boolean) map.get("obj");
        } catch (Exception e1) {
            e1.printStackTrace();
            e=false;
        }
        if (null!=event&&BroadCastEvent.BB_GROUP_REFUL.equals(event)&&e) {
            mAdd_group.setText("加入群组");
            mGroupBeanData.getData().setJoin(false);
        }
    }
    /**
     * 加入或退出群组
     */
    private void addGroup(boolean res){
        MessageParam messageParam=new MessageParam();
        messageParam.setGroupid(groupid);
        messageParam.setUserId(userPrefUtils.getUser().getId() + "");
        if(res)
            new FriendApi(this).AddGroupQuit(messageParam,this);
        else
            new FriendApi(this).quitGroup(messageParam,this);

    }

    /**
     * 群组详情
     */
    private void getGroupDetails(){
        MessageParam messageParam=new MessageParam();
        messageParam.setGroupid(groupid);
        messageParam.setUserId(userPrefUtils.getUser().getId() + "");
        new FriendApi(this).getGroupDetall(messageParam,this);
    }
    @Override
    public void onStart(String tag) {
        showDialog("正在加载...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (JSONHelper.isSuccess(response)) {
            if(tag.equals(ApiConfig.APIURL.URL_GROUP_APPLY)||tag.equals(ApiConfig.APIURL.URL_GROUP_USERS_QUIT)){
                RunData data = JsonUtil.getMode(response,
                        RunData.class);
                if (tag.equals(ApiConfig.APIURL.URL_GROUP_APPLY)) {
                    if("加入成功".equals(data.getData())) {
                        mGroupBeanData.getData().setJoin(true);
                        mAdd_group.setText("聊天");
                        HashMap<String, Object> hashMap = new HashMap<String, Object>();
                        hashMap.put("event", BroadCastEvent.BB_GROUP_REFUL);
                        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON, ThreadEnforcer.MAIN).post(hashMap);
                        StringUtils.postToast(GroupDetailsActivity.this,"加入群组成功");
                    }else{
                        StringUtils.postToast(GroupDetailsActivity.this,data.getData()+"");
                    }

                }else if(tag.equals(ApiConfig.APIURL.URL_GROUP_USERS_QUIT)){
                    if("退群成功".equals(data.getData())) {
                        mGroupBeanData.getData().setJoin(false);
                        mAdd_group.setText("加入群组");
                    }else{
                        StringUtils.postToast(GroupDetailsActivity.this,data.getData()+"");
                    }
                }
            }else {
                 mGroupBeanData = JsonUtil.getMode(response,
                        GroupBeanData.class);
                if (null != mGroupBeanData) {
                    mUserList = mGroupBeanData.getData().getUsers();
                    menberAdapter.setList(mUserList);
                    mGridview.setAdapter(menberAdapter);
                    new GlideUtils().Glidde_loadCropCircleImage(GroupDetailsActivity.this, mGroupBeanData.getData().getLogo() + "", mGroup_icon);
                    mGroup_details.setText(mGroupBeanData.getData().getRemark());
                    if (mGroupBeanData.getData().isJoin()) {
                        mAdd_group.setText("聊天");
                    } else {
                        mAdd_group.setText("加入群组");
                    }
                }
            }
        }else if(JSONHelper.getResultCode(response) == 7006){
            StringUtils.postToast(GroupDetailsActivity.this,JSONHelper.getResultMsg(response)+"");
        }else{
            StringUtils.postToast(GroupDetailsActivity.this,JSONHelper.getResultMsg(response)+"");
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        ResultCode.toastVolleyError(this, volleyError);
    }






}
